<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>WebSocket Chat</title>
</head>
<body>
<script type="text/javascript">
    var socket;
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
         socket = new WebSocket("ws://localhost:5454/ws");
        //socket = new WebSocket("ws://101.201.107.140:5454/ws");
        socket.onmessage = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + '\n' + event.data
        };
        socket.onopen = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = "连接开启!";
            send(document.getElementById('register').value)
        };
        socket.onclose = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + "连接被关闭";
        };
    } else {
        alert("你的浏览器不支持 WebSocket！");
    }

    function send(registerRequest) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(registerRequest);
        } else {
            alert("连接没有开启.");
        }
    }
</script>
<form onsubmit="return false;">
    <h3>WebSocket 聊天室：</h3>
    <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
    <br>
    <textarea id="register" style="width: 500px; height: 200px;">
            {
                  "option":"REGISTER",
                   "lessonId":1,
                   "name":"王万成",
                   "account":"15011SS",
                   "role": "STUDENT",
                   "activityId": 1,

            }

        </textarea>
    <input type="button" value="注册" onclick="send(this.form.register.value)">
    <br>
    <textarea name="registerRequest" style="width: 500px; height: 200px;">
         {
                  "option":"ANSWER_SEND",
                   "activityId": "1",
                    "questionId": "1",
                    "answer":["A"],
                    "rightAnswer":["A"],
                    "currentDate":1551524410000
            }
    </textarea>
    <input type="button" value="发送消息" onclick="send(this.form.registerRequest.value)">
    <input type="button" onclick="javascript:document.getElementById('responseText').value=''" value="清空聊天记录">
</form>
<br>
<br>
<p>数据结构:</p>
<p>
    {"msg":"注册成功","code":"SOCKET_SUCCESS","role":"STUDENT","name":"王小样","lessonId":1,"account":"123","option":"REGISTER"}
    消息发送人身份,只用在注册时传入
    role:STUDENT,TEACHER
    消息发送行为  REGISTER为注册  SEND为发送信息  DESTROY为
    option: REGISTER SEND DESTROY
</p>
</body>
</html>